$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        frame:null,
        init:function () {
            this.frame = $('iframe');
            if (type == 'schools'){
                $('#schools').select2();
            } else if (type == 'teachers') {
                $('#schoolId,#teachers').select2();
            }

            this.listener();
        },
        listener:function () {
            var $this = this;
            $('#btn_view').click(function () {
                $this.funObj().viewUsage();
            });
            $('#schoolId').change(function () {
                $this.funObj().getTchData($(this).val());
            })
            //加载完毕后关闭提示
            $this.frame.load(function(){
                window.top.swal.close();
            });
        },
        funObj:function () {
            var $this = this;
            return {
                viewUsage:function () {
                    $this.frame.attr('src', '');
                    setTimeout(function () {
                        var startTime = $('#startTime').val();
                        var endTime   = $('#endTime').val();
                        if (type == 'allSchool') {
                            $this.frame.attr('src', baseUrl + 'stats_tch/statsTchDetail/'+type+'?' + xhTool.parseParam({
                                startTime: startTime,
                                endTime: endTime
                            }));
                        } else if (type == 'schools') {
                            var schools = $('#schools').val();
                            $this.frame.attr('src', baseUrl + 'stats_tch/statsTchDetail/'+type+'?' + xhTool.parseParam({
                                startTime: startTime,
                                endTime: endTime,
                                schools:schools
                            }));
                        } else if (type == 'teachers') {
                            var schoolId = $('#schoolId').val();
                            if (!schoolId) {
                                window.top.xhTop.showError('错误','学校必选');
                                // console.log($('#btn_view'));
                                xhExtra.setBtnNormal($('#btn_view'));
                                return false;
                            }
                            var teachers = $('#teachers').val();
                            $this.frame.attr('src', baseUrl + 'stats_tch/statsTchDetail/'+type+'?' + xhTool.parseParam({
                                startTime: startTime,
                                endTime: endTime,
                                schoolId:schoolId,
                                teachers:teachers
                            }));
                        }
                        window.top.swal({
                            title: '考核统计执行中！请耐心等待...',
                            showConfirmButton: false,
                            type: 'success',
                            allowOutsideClick: false
                        });
                        setTimeout(function () {
                            window.top.swal.close();
                        }, 30 * 1000);
                        xhExtra.setBtnNormal($('#btn_view'));
                    }, 0);
                },
                getTchData:function (schoolId) {
                    if (!schoolId) {
                        $('#teachers').empty().append('<option value="0">全部</option>');
                        return false;
                    }
                    xhExtra.ajaxRequest('stats_tch/getTeacher',{schoolId:schoolId},{},function (data) {
                        var str = '<option value="0" >全部</option>';
                        $.each(data,function(id,text){
                            str += '<option value="'+id+'" >'+text+'</option>';
                        });
                        $('#teachers').empty().append(str);
                    })
                }
            }
        }
    }
};